#define DEF_ORG_FREQ_TABLE

#include "BuzzerMusic.h"
#include "Delay.h"
#include "LCD1602.h"

#define PER_TIME 54

uchar code Music[] = {
	// 1
	P, 16 * 4,
	// 2
	LL7, 16, LL7, 16, LL7, 16, LL7, 4, L3, 4, L2, 4, L2, 4,
	// 3
	LL7, 16, LL7, 16, LL7, 16, LL7, 8, L1, 8,
	// 4
	LL7, 16, LL7, 16, LL7, 16, LL7, 4, L3, 4, L2, 4, L2, 4,
	// 5
	LL7, 16, LL7, 16, LL7, 16, LL7, 8, L1_, 8,
	// 6
	LL7, 16, LL7, 16, LL7, 16, LL7, 4, L3, 4, L2, 4, L2, 4,
	// 7
	LL7, 16, LL7, 16, LL7, 16, LL7, 8, L1, 8,
	// 8
	LL7, 16, LL7, 16, LL7, 16, LL7, 4, L3, 4, L2, 4, L2, 4,
	// 9
	LL7, 8, M4_, 8, LL7, 8, M5, 8, LL7, 8, H4_, 8, HH3, 16,
	// 10
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 11
	/*P, 64,*/ L3, 16, L3, 16, L4, 16, L4, 8, L4, 8, 
	// 12
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 13
	/*P, 64,*/ L3, 16, L3, 16, L4, 16, L4, 8, L4, 8, 
	// 14
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 15
	/*P, 64,*/ L3, 16, L3, 16, L4, 16, L4, 8, L4, 8,
	// 16
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 17
	/*P, 64,*/ L3, 16, L3, 16, L1, 4, L4, 4, L1, 4, L4, 4, L1, 4, L4, 4, L1, 4, L4, 4,  
	// 18
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, H3, 8, H2, 8,
	// 19
	M7, 16, M3, 16, M7, 8, M6, 2, M7, 2, M6, 4, M5, 8, M4_, 8,
	// 20
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, M6, 8, M5, 8,
	// 21
	M4_, 8, M3, 8, M4, 8, M5, 8, NO_EMPTY | M4, 2, NO_EMPTY | M5, 2, M4, 4, M3, 8, M2_, 8, M4, 8,
	// 22
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, H3, 8, H2, 8,
	// 23
	M7, 16, M3, 16, M7, 8, M6, 2, M7, 2, M6, 4, M5, 8, M4_, 8,
	// 24
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, M6, 8, M5, 8,
	// 25 '''
	M4_, 10, P, 6, M5, 10, P, 6, M6, 10, P, 6, M7, 10, P, 6,
	// 26
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, HH3, 8, HH2, 8,
	// 27
	H7, 16, H3, 16, H7, 8, H6, 2, H7, 2, H6, 4, H5, 8, H4_, 8,
	// 28
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, H6, 8, H5, 8,
	// 29
	H4_, 8, H3, 8, H4, 8, H5, 8, NO_EMPTY | H4, 2, NO_EMPTY | H5, 2, H4, 4, H3, 8, H2_, 8, H4, 8,
	// 30
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, HH3, 8, HH2, 8,
	// 31
	H7, 16, H3, 16, H7, 8, H6, 2, H7, 2, H6, 4, H5, 8, H4_, 8,
	// 32
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, H6, 8, H5, 8, 
	// 33 '''
	H4_, 10, P, 6, H5, 10, P, 6, H6, 10, P, 6, H7, 10, P, 6,
	// 34
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 35
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 36
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H2, 8, H3, 8,
	// 37
	H4_, 8, H5, 8, H6, 8, H7, 8, H3, 16, H7, 8, HH2, 8,
	// 38
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 39
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 40
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H2, 8, H3, 8,
	// 41
	H4_, 8, H5, 8, H6, 8, H7, 8, H3, 16, H7, 8, HH2, 8,
	// 42
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 43
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 44
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H2, 8, H3, 8,
	// 45
	H4_, 8, H5, 8, H6, 8, H7, 8, H3, 16, H7, 8, HH2, 8,
	// 46
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 47
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, HH3, 8, HH4, 8,
	// 48
	HH5, 8, HH4_, 8, HH3, 8, HH2, 8, H7, 16, H6, 8, H7, 8,
	// 49
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, M7, 8, H2, 8,
	// 50
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 51
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 52
	M6, 8, M5, 8, M4_, 8, M2, 8, M3, 16, M2, 8, M3, 8,
	// 53
	M4_, 8, M5, 8, M6, 8, M7, 8, M3, 16, M7, 8, H2, 8,
	// 54
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 55
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 56
	M6, 8, M5, 8, M4_, 8, M2, 8, M3, 16, M2, 8, M3, 8,
	// 57
	M4_, 8, M5, 8, M6, 8, M7, 8, M3, 16, M7, 8, H2, 8,
	// 58
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 59
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 60
	M6, 8, M5, 8, M4_, 8, M2, 8, M3, 16, M2, 8, M3, 8,
	// 61
	M4_, 8, M5, 8, M6, 8, M7, 8, M3, 16, M7, 8, H2, 8,
	// 62
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, M6, 8, M7, 8,
	// 63
	H2, 8, H3, 8, M7, 8, M6, 8, M7, 16, H3, 8, H4_, 8,
	// 64
	H5, 8, H4_, 8, H3, 8, H2, 8, M7, 16, M6, 8, M7, 8,
	// 65
	M6, 8, M5, 8, M4_, 8, M2, 8, M3, 16, L3, 16,
	// 66
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 67
	/*P, 64,*/ L3, 16, L3, 16, L4, 16, L4, 8, L4, 8, 
	// 68
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 69
	/*P, 64,*/ L3, 16, L3, 16, L4, 16, L4, 8, L4, 8, 
	// 70
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 71
    /*P, 64,*/ L3, 16, L3, 16, L4, 16, L4, 8, L4, 8, 
	// 72
	/*P, 64,*/ L3, 16, L3, 16, L3, 16, L3, 16, 
	// 73
	/*P, 64,*/ L3, 16, L3, 16, L1, 4, L4, 4, L1, 4, L4, 4, L1, 4, L4, 4, L1, 4, L4, 4,
	// 74
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, H3, 8, H2, 8,
	// 75
	M7, 16, M3, 16, M7, 8, M6, 2, M7, 2, M6, 4, M5, 8, M4_, 8,
	// 76
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, M6, 8, M5, 8,
	// 77
	M4_, 8, M3, 8, M4, 8, M5, 8, NO_EMPTY | M4, 2, NO_EMPTY | M5, 2, M4, 4, M3, 8, M2_, 8, M4, 8,
	// 78
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, H3, 8, H2, 8,
	// 79
	M7, 16, M3, 16, M7, 8, M6, 2, M7, 2, M6, 4, M5, 8, M4_, 8,
	// 80
	M3, 8, M4_, 8, M5, 8, M6, 8, M7, 16, M6, 8, M5, 8,
	// 81 '''
	H4_, 10, P, 6, H5, 10, P, 6, H6, 10, P, 6, H7, 10, P, 6,
	// 82
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, HH3, 8, HH2, 8,
	// 83
	H7, 16, H6, 16, H7, 8, H6, 2, H7, 2, H6, 4, H5, 8, H4, 8,
	// 84
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, H6, 8, H5, 8,
	// 85
	H4_, 8, H3, 8, H4, 8, H5, 8, NO_EMPTY | H4, 2, NO_EMPTY | H5, 2, H4, 4, H3, 8, H2_, 8, H4, 8,
	// 86
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, HH3, 8, HH2, 8,
	// 87
	H7, 16, H3, 16, H7, 8, H6, 2, H7, 2, H6, 4, H5, 8, H4_, 8,
	// 88
	H3, 8, H4_, 8, H5, 8, H6, 8, H7, 16, H6, 8, H5, 8, 
	// 89 '''
	H4_, 10, P, 6, H5, 10, P, 6, H6, 10, P, 6, H7, 10, P, 6,
	// 90
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 91
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 92
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H2, 8, H3, 8,
	// 93
	H4_, 8, H5, 8, H6, 8, H7, 8, H3, 16, H7, 8, HH2, 8,
	// 94
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 95
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 96
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H2, 8, H3, 8,
	// 97
	H4_, 8, H5, 8, H6, 8, H7, 8, H3, 16, H7, 8, HH2, 8,
	// 98
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 99
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 100
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H2, 8, H3, 8,
	// 101
	H4_, 8, H5, 8, H6, 8, H7, 8, H3, 16, H7, 8, HH2, 8,
	// 102
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, H6, 8, H7, 8,
	// 103
	HH2, 8, HH3, 8, H7, 8, H6, 8, H7, 16, HH3, 8, HH4, 8,
	// 104
	HH5, 8, HH4_, 8, HH3, 8, HH2, 8, H7, 16, H6, 8, H7, 8,
	// 105
	H6, 8, H5, 8, H4_, 8, H2, 8, H3, 16, H1, 8, HH2_, 8,
	// 106
	HH2_, 8, HH4, 8, HH1, 8, H6_, 8, HH1, 16, H6, 8, HH1, 8,
	// 107
	HH2_, 8, HH4, 8, HH1, 8, H6_, 8, HH1, 16, H6, 8, HH1, 8,
	// 108
	H6_, 8, H5_, 8, H5, 8, H2_, 8, H4, 16, H2, 8, H4, 8,
	// 109
	H5, 8, H5_, 8, H6_, 8, HH1, 8, H4, 16, HH1, 8, HH2_, 8,
	// 106
	HH2_, 8, HH4, 8, HH1, 8, H6_, 8, HH1, 16, H6, 8, HH1, 8,
	// 107
	HH2_, 8, HH4, 8, HH1, 8, H6_, 8, HH1, 16, H6, 8, HH1, 8,
	// 108
	H6_, 8, H5_, 8, H5, 8, H2_, 8, H4, 16, H2, 8, H4, 8,
	// 109
	H5, 8, H5_, 8, H6_, 8, HH1, 8, H4, 16, HH1, 8, HH2_, 8,
	// 110
	HH2_, 8, HH4, 8, HH1, 8, H6_, 8, HH1, 16, H6, 8, HH1, 8,
	// 111
	HH2_, 8, HH4, 8, HH1, 8, H6_, 8, HH1, 16, H6, 8, HH1, 8,
	// 112
	H6_, 8, H5_, 8, H5, 8, H2_, 8, H4, 16, H2, 8, H4, 8,
	// 113
	H5, 8, H5_, 8, H6_, 8, HH1, 8, H4, 16, /*HH1, 8, HH2_, 8,*/ H1, 8, H2_, 8,

	// 114
	H2_, 8, H4, 8, H1, 8, M6_, 8, H1, 16, M6, 8, H1, 8,
	// 115
	H2_, 8, H4, 8, H1, 8, M6_, 8, H1, 16, M6, 8, H1, 8,
	// 116
	M6_, 8, M5_, 8, M5, 8, M2_, 8, M4, 16, M2, 8, M4, 8,
	// 117
	M5, 8, M5_, 8, M6_, 8, H1, 8, M4, 16, H1, 8, H2_, 8,
	// 118
	H2_, 8, H4, 8, H1, 8, M6_, 8, H1, 16, M6, 8, H1, 8,
	// 119
	H2_, 8, H4, 8, H1, 8, M6_, 8, H1, 16, M6, 8, H1, 8,
	// 120
	M6_, 8, M5_, 8, M5, 8, M2_, 8, M4, 16, M2, 8, M4, 8,
	// 121
	M5, 8, M5_, 8, M6_, 8, H1, 8, M4, 16,

	/*
	// 114
	H5_, 8, H5, 8, H5, 8, H6_, 8, H5, 16, H5, 8, H5_, 8,
	// 115
	H6_, 8, H6, 8, H5, 8, H6, 8, HH1, 16, H5, 8, HH1, 8,
	// 116
	H4, 8, H2_, 8, H1, 8, H2, 8, H1, 16, H1, 8, H4, 8,
	// 117
	H2_, 8, H4, 8, H5, 8, H5_, 8, H1, 16, H5, 8, HH2_, 8,
	// 118
	H5_, 8, H5, 8, H5, 8, H6_, 8, H5, 16, H5, 8, H5_, 8,
	// 119
	H6_, 8, H6, 8, H5, 8, H6, 8, HH1, 16, H5_, 8, H6, 8,
	// 120
	H5_, 8, H5, 8, H4, 8, H2_, 8, H1, 16, M6_, 8, H1, 8,
	// 121
	M6_, 8, M5_, 8, M5, 8, M2_, 8, M4, 16,	 */

	P, 64,
};

#define FREQ_SELECT_OFFSET -3

uint musicCnt = sizeof(Music) / sizeof(uchar);
uint musicInd, musicFreq;
uint musicMineute, musicSecond, musicMs;

void main() {
	uint musicDelay = 0, musicDelayEnd, freqSelect;
	uchar noDelay = 0;

	TMOD = 0x01;
	TF0 = 0;
	TR0 = 1;
	TH0 = 0xFF;
	TL0 = 0xFF;
	ET0 = 1;
	PT0 = 0;
	
	EA = 1;

	LCD_Init();
	LCD_ShowString(1, 5, "Hz");
	LCD_ShowString(2, 1, "00:00");

	while(1) {
		if(musicInd < musicCnt) {
			freqSelect = Music[musicInd++];
#ifdef FREQ_SELECT_OFFSET
			if(freqSelect != 0)
				freqSelect += FREQ_SELECT_OFFSET;
#endif
			noDelay = (freqSelect & 0x80) == 0x80;
			musicFreq = FreqTable[freqSelect & 0x7F];
			musicDelay = Music[musicInd++] / 2 * PER_TIME;
			musicDelayEnd = musicDelay / 6;
			
			musicMs += musicDelay;
			while(musicMs >= 1000) {
				musicSecond++;
				musicMs -= 1000;
				LCD_ShowNum(2, 4, musicSecond, 2);
				musicDelay -= 8;
			}
			while(musicSecond >= 60) {
				musicMineute++;
				musicSecond -= 60;
				LCD_ShowNum(2, 4, musicSecond, 2);
				LCD_ShowNum(2, 1, musicMineute, 2);
				musicDelay -= 16;
			}

			musicDelay = musicDelay * 7 / 8;
			musicDelayEnd = musicDelayEnd * 7 / 8;

			if(!noDelay)
				musicDelay -= musicDelayEnd;
			LCD_ShowNum(1, 1, OrgFreqTable[freqSelect], 4);

			if(!P3_3) {
				musicDelay /= 4;
				musicDelayEnd /= 4;
			}
			Delay(musicDelay - 8);
			if(!noDelay) {
				TR0 = 0;
				Delay(musicDelayEnd);
				TR0 = 1;
			}
		}
	}
}

sbit BUZZER = P1^5;
void Timer0_Routine() interrupt 1
{
	if(musicFreq) {
		 TH0 = musicFreq / 256;
		 TL0 = musicFreq % 256;
		 BUZZER = !BUZZER;
	}
}